{
 "cells": [
  {
   "metadata": {
    "collapsed": true
   },
   "cell_type": "markdown",
   "source": [
    "#### 1.模型调用的分类\n",
    "\n",
    "角度1：按模型功能的不同\n",
    "    非对话模型：(LLMs、Text Model)\n",
    "    对话模型：(Chat Models) (推荐)\n",
    "    嵌入模型：（Embedding Models)\n",
    "\n",
    "角度2：按照模型调用时，参数书写的位置不同(api-key、base_url、model-name)\n",
    "    硬编码方式\n",
    "    环境变量方式\n",
    "    使用配置文件的方式(推荐)\n",
    "\n",
    "角度3：具体API的调用\n",
    "    使用LangChain提供的API(推荐)\n",
    "    使用OpenAI官方API\n",
    "    使用其它平台提供的API"
   ],
   "id": "41d616f2744610a3"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "#### 2.非对话模型调用\n",
    "    输入：文本字符串\n",
    "    输出：文本字符串\n",
    "    适用场景：仅需单词文本生成任务，如摘要生成、翻译、代码生成、单词回答"
   ],
   "id": "edca3af3c8ef38de"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import os\n",
    "from langchain_openai import OpenAI\n",
    "\n",
    "os.environ['OPENAI_BASE_URL'] = 'https://vip.apiyi.com/v1'\n",
    "os.environ['OPENAI_API_KEY'] = 'sk-xU64G4hXJ4L47ko3764958119dB245D2BdEcE528767dA1Da'\n",
    "\n",
    "llm = OpenAI()\n",
    "response = llm.invoke(input=\"写一首关于春天的诗\")\n",
    "print(response)"
   ],
   "id": "5320b80b2fcd030d",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "#### 3.Chat Models(对话模型)\n",
    "    输入：接受消息列表List[BaseMessage]或PromptValue，每条消息需指定角色(如SystemMessage、HumanMessage)\n",
    "    输出：总是返回带角色的消息对象(BaseMessage子类)，通常是AIMessage\n",
    "    适用场景：对话系统(客服机器人、长期交互AI助手)"
   ],
   "id": "de4b993edbdeb277"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import os\n",
    "from langchain_openai import ChatOpenAI\n",
    "from langchain_core.messages import SystemMessage, HumanMessage\n",
    "\n",
    "os.environ['OPENAI_BASE_URL'] = 'https://vip.apiyi.com/v1'\n",
    "os.environ['OPENAI_API_KEY'] = 'sk-xU64G4hXJ4L47ko3764958119dB245D2BdEcE528767dA1Da'\n",
    "\n",
    "chat_model = ChatOpenAI(model=\"gpt-4o-mini\")\n",
    "messages = [\n",
    "    SystemMessage(content=\"我是人工只能助手，我叫小智\"),\n",
    "    HumanMessage(content=\"你好，我是小明，很高兴认识你\")\n",
    "]\n",
    "\n",
    "response = chat_model.invoke(messages)\n",
    "print(type(response))\n",
    "print(response.content)"
   ],
   "id": "2ddcf20d35409ad1",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "#### 4.Embedding Model(嵌入模型)\n",
    "     Embedding Model：也叫文本嵌入模型，这些模型将文本作为输入并返回浮点数列表，也就是Embedding。"
   ],
   "id": "6b9621973dc01b63"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import os\n",
    "from langchain_openai import OpenAIEmbeddings\n",
    "\n",
    "os.environ['OPENAI_BASE_URL'] = 'https://vip.apiyi.com/v1'\n",
    "os.environ['OPENAI_API_KEY'] = 'sk-xU64G4hXJ4L47ko3764958119dB245D2BdEcE528767dA1Da'\n",
    "\n",
    "embeddings_model = OpenAIEmbeddings(model=\"text-embedding-ada-002\")\n",
    "embeddings_response = embeddings_model.embed_query(\"我是文档中的数量\")\n",
    "print(embeddings_response)"
   ],
   "id": "d54dd3109f392b77",
   "outputs": [],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
